package westh.ilib.service.surveyReport.documentSearch;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.document.Document;
/**
 * document preparation, transform xml into trec for Lemur/Indri
 * @author Zhang Yanxing
 * 2010-8-19
 */
// transform the file format into the one indexer needs
public class docTransformer {
	// transform .xml into the trectext format, which was used in Lemur
	public static void xml2trec(String infilepath, String outfilepath, String idmappath) {
		File file = new File(infilepath);
		File[] filelist;

		if (!file.exists()) {
			System.out.println("Dir not find: " + infilepath);
			return;
		}

		File outdir = new File(outfilepath);
		if (!outdir.exists()) {
			try {
				outdir.mkdir();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (!file.isDirectory()) {
			System.out.println(" <dir> " + file.getName());
			return;
		} else if (file.isDirectory()) {
			filelist = file.listFiles();

			try {
				BufferedWriter writer = new BufferedWriter(new FileWriter(
						new File(idmappath)));
				for (int i = 0; i < filelist.length; i++) {
					if (!filelist[i].isDirectory()) {
						BufferedReader br = new BufferedReader(new FileReader(
								filelist[i]));
						char[] cbuf = new char[(int) filelist[i].length()];
						while (br.ready()) {
							br.read(cbuf, 0, (int) filelist[i].length());
						}

						String fileName = filelist[i].getName().substring(0,
								filelist[i].getName().length() - 4);
						writer.write(Integer.toString(i) + "\t" + fileName);
						writer.newLine();

						String outFileName = outfilepath + fileName + ".trec";
						System.out.println(outFileName);
						File outFile = new File(outFileName);
						if (!outFile.exists()) {
							outFile.createNewFile();
						} else {
							continue;
						}

						BufferedWriter bw = new BufferedWriter(new FileWriter(
								outFile));
						bw.write("<DOC>");
						bw.newLine();
						bw.write("<DOCNO>");
						bw.write(Integer.toString(i));
						bw.write("</DOCNO>");
						bw.newLine();
						bw.write("<TEXT>");
						bw.newLine();

						String str = new String(cbuf);
						String titleEx = "<title>(.+?)</title>";
						Pattern titlePattern = Pattern.compile(titleEx,
								Pattern.MULTILINE | Pattern.DOTALL);
						Matcher titleMatcher = titlePattern.matcher(str);
						if (titleMatcher.find()) {
							String title = titleMatcher.group(0);
							title = title.substring(7, title.length() - 8);
							bw.write(title);
							bw.newLine();
						}

						String keywordEx = "<keywords>(.+?)</keywords>";
						Pattern keywordPattern = Pattern.compile(keywordEx,
								Pattern.MULTILINE | Pattern.DOTALL);
						Matcher keywordMatcher = keywordPattern.matcher(str);
						if (keywordMatcher.find()) {
							String keyword = keywordMatcher.group(0);
							keyword = keyword.substring(10,
									keyword.length() - 11);
							if (keyword.equalsIgnoreCase("null")) {
								keyword = "";
							} else {
								bw.write(keyword);
								bw.newLine();
							}
						}

						String abstractEx = "<abstract>(.+?)</abstract>";
						Pattern abstractPattern = Pattern.compile(abstractEx,
								Pattern.MULTILINE | Pattern.DOTALL);
						Matcher abstractMatcher = abstractPattern.matcher(str);
						if (abstractMatcher.find()) {
							String summary = abstractMatcher.group(0);
							summary = summary.substring(10,
									summary.length() - 11);
							bw.write(summary);
							bw.newLine();
						}

						String bodyEx = "<body>(.+?)</body>";
						Pattern bodyPattern = Pattern.compile(bodyEx,
								Pattern.MULTILINE | Pattern.DOTALL);
						Matcher bodyMatcher = bodyPattern.matcher(str);
						if (bodyMatcher.find()) {
							String body = bodyMatcher.group(0);
							body = body.substring(6, body.length() - 7);
							bw.write(body);
							bw.newLine();
						}

						bw.write("</TEXT>");
						bw.write("</DOC>");
						bw.close();
					} else {
						System.out.println("<dir>" + filelist[i].getName());
					}
				}
				writer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(filelist.length);
		}
	}

	// transform .xml into Document used in Lucene
	public static Document[] docPreparation(String infilepath) {
		File file = new File(infilepath);
		File[] filelist;

		if (!file.exists()) {
			System.out.println("Dir not find: " + infilepath);
			return null;
		}

		if (!file.isDirectory()) {
			System.out.println(" <dir> " + file.getName());
			return null;
		} else if (file.isDirectory()) {
			filelist = file.listFiles();
			Document[] docs = new Document[filelist.length];

			try {
				for (int i = 0; i < filelist.length; i++) {
					if (!filelist[i].isDirectory()) {
						docs[i] = FileDocument.Document(filelist[i]);
					} else {
						System.out.println("<dir>" + filelist[i].getName());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(filelist.length);
			return docs;
		}
		return null;
	}

	public static void main(String[] args) {
		String infilepath = "E:\\zhyx\\SurveyReport\\xml\\";

		String outfilepath = "E:\\zhyx\\SurveyReport\\trec\\";
		String idmappath = "E:\\zhyx\\SurveyReport\\idmap.txt";

		docTransformer.xml2trec(infilepath, outfilepath, idmappath);

		/*
		Document[] docs = docTransformer.docPreparation(infilepath);
		 
		System.out.println(docs.length);

		// unit test
		for (int i = 0; i < docs.length; i++) {
			if (docs[i] == null) {
				// todo: exception
				continue;
			}
			System.out.println(i);
			System.out.println(docs[i].getField("path"));
			System.out.println(docs[i].getField("year"));
			System.out.println(docs[i].getField("conf"));
			System.out.println(docs[i].getField("title"));
			System.out.println(docs[i].getField("keyword"));
			System.out.println(docs[i].getField("summary"));
			System.out.println(docs[i].getField("contents"));
		}
		*/
	}
}
